package com.example.speedwaymanager;

import java.util.ArrayList;
import java.util.Arrays;

import android.annotation.SuppressLint;
import android.app.ListActivity;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.music.TouchInterceptor;

@SuppressLint("NewApi")
public class SquadActivity extends ListActivity implements OnItemSelectedListener {
		    
	private static final String TAG = "SquadActivity";
	CGameManager _gameManager = CGameManager.getInstance();
	@SuppressWarnings("unused")
	private CTeam _teamSquadSelected;
	private Object[] sArray;
	ArrayAdapter<CRider> _adp;

	private TouchInterceptor mList;
	private TouchInterceptor.DropListener mDropListener =
			    new TouchInterceptor.DropListener() {
			        public void drop(int from, int to) {
			            System.out.println("Droplisten from:"+from+" to:"+to);

			            //Assuming that item is moved up the list
			            int direction = -1;
			            int loop_start = from;
			            int loop_end = to;

			            //For instance where the item is dragged down the list
			            if(from < to) {
			            	direction = 1;
			            }

			            Object target = sArray[from];

			            for(int i=loop_start;i!=loop_end;i=i+direction){
			            	sArray[i] = sArray[i+direction];
			            }

			            sArray[to] = target;

			            System.out.println("Changed array is:"+Arrays.toString(sArray));
			            ((BaseAdapter) mList.getAdapter()).notifyDataSetChanged();
			        }
			    };
			    
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_squad);
		// Show the Up button in the action bar.
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
			getActionBar().setDisplayHomeAsUpEnabled(true);
		}
		UIInit();
		
	}
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		String selection = sArray[position].toString();
		Toast.makeText(this, selection, Toast.LENGTH_SHORT).show();
	}

	public void UIInit() {		
		TextView textView = (TextView) findViewById(R.id.textView_teamName);
		_teamSquadSelected = _gameManager.get_selectedSquadTeam();
		textView.setText(_teamSquadSelected.toString());				
		listViewInit();						
		mList = (TouchInterceptor) getListView();
        mList.setDropListener(mDropListener);        
        registerForContextMenu(mList);		
	}

	public void listViewInit()
	{				
		_teamSquadSelected.get_coach().createSquad();
		ArrayList<CRider> riders = _teamSquadSelected.get_squad();
		sArray = new Object[riders.size()];
		sArray = riders.toArray();		
		_adp = new ArrayAdapter(this, R.layout.listrow, sArray);
		setListAdapter(_adp);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_squad, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			// This ID represents the Home or Up button. In the case of this
			// activity, the Up button is shown. Use NavUtils to allow users
			// to navigate up one level in the application structure. For
			// more details, see the Navigation pattern on Android Design:
			//
			// http://developer.android.com/design/patterns/navigation.html#up-vs-back
			//
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public void confirmSquadClicked(View view)
	{
		String str = Arrays.toString(sArray);
		_gameManager.get_selectedSquadTeam().get_coach().setSquad(sArray);
		this.finish();
	}
	
	public void cancelClicked(View view)
	{
		this.finish();
	}
	@Override
	public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
			long arg3) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void onNothingSelected(AdapterView<?> arg0) {
		// TODO Auto-generated method stub
		
	}

}
